package com.muyu.danmo.controller;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.web.page.TableDataInfo;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.DanmoGalleryDetailsLikes;
import com.muyu.danmo.service.IDanmoGalleryDetailsLikesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 画廊点赞Controller
 *
 * @author yangle
 * @date 2024-10-23
 */
@RestController
@RequestMapping("/galleryDetailsLikes")
public class DanmoGalleryDetailsLikesController extends BaseController {
    @Autowired
    private IDanmoGalleryDetailsLikesService danmoGalleryDetailsLikesService;

    /**
     * 查询画廊点赞列表
     */
//    @RequiresPermissions("system:galleryDetailsLikes:list")
    @GetMapping("/list")
    public Result<TableDataInfo<DanmoGalleryDetailsLikes>> list(DanmoGalleryDetailsLikes danmoGalleryDetailsLikes) {
        startPage();
        List<DanmoGalleryDetailsLikes> list = danmoGalleryDetailsLikesService.selectDanmoGalleryDetailsLikesList(danmoGalleryDetailsLikes);
        return getDataTable(list);
    }

    /**
     * 导出画廊点赞列表
     */
//    @RequiresPermissions("system:galleryDetailsLikes:export")
    @Log(title = "画廊点赞", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DanmoGalleryDetailsLikes danmoGalleryDetailsLikes) {
        List<DanmoGalleryDetailsLikes> list = danmoGalleryDetailsLikesService.selectDanmoGalleryDetailsLikesList(danmoGalleryDetailsLikes);
        ExcelUtil<DanmoGalleryDetailsLikes> util = new ExcelUtil<DanmoGalleryDetailsLikes>(DanmoGalleryDetailsLikes.class);
        util.exportExcel(response, list, "画廊点赞数据");
    }

    /**
     * 获取画廊点赞详细信息
     */
//    @RequiresPermissions("system:galleryDetailsLikes:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return success(danmoGalleryDetailsLikesService.selectDanmoGalleryDetailsLikesById(id));
    }

    /**
     * 新增画廊点赞
     */
//    @RequiresPermissions("system:galleryDetailsLikes:add")
    @Log(title = "画廊点赞", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody DanmoGalleryDetailsLikes danmoGalleryDetailsLikes) {
        return toAjax(danmoGalleryDetailsLikesService.insertDanmoGalleryDetailsLikes(danmoGalleryDetailsLikes));
    }

    /**
     * 修改画廊点赞
     */
//    @RequiresPermissions("system:galleryDetailsLikes:edit")
    @Log(title = "画廊点赞", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody DanmoGalleryDetailsLikes danmoGalleryDetailsLikes) {
        return toAjax(danmoGalleryDetailsLikesService.updateDanmoGalleryDetailsLikes(danmoGalleryDetailsLikes));
    }

    /**
     * 删除画廊点赞
     */
//    @RequiresPermissions("system:galleryDetailsLikes:remove")
    @Log(title = "画廊点赞", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(danmoGalleryDetailsLikesService.deleteDanmoGalleryDetailsLikesByIds(ids));
    }


    @GetMapping("/like/{trainingInformationId}")
    public Result likeGalleryDetails(@PathVariable Long detailsId) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null || loginUser.getUserid() == null) {
            return Result.notLoggedIn("请登录");
        }
        Long userId = loginUser.getUserid();
        if (userId.equals(detailsId)) {
            return Result.error("不能点赞自己");
        }

        return Result.success(danmoGalleryDetailsLikesService.likeGalleryDetails(userId, detailsId));
    }

    /**
     * @param detailsId
     * @return
     * @description: 获取点赞数量
     */
    @GetMapping("/likes/{detailsId}")
    public Long getLikesCount(@PathVariable Long detailsId) {
        return danmoGalleryDetailsLikesService.getLikesCount(detailsId);
    }
}
